#include "User.h"
#include "Seat.h"
#include "assert.h"

class User;
struct calcSeatWin
{
	int seatid;
	bool HaveUser; 
	int role;
	int pokerSize;
};

Table::Table(int _t){
	if( _t < 0 ) {
		assert( false );
	}
	tid = _t;
	pointTotal = 0;
}
Table::~Table(){}

//用户基本操作
int Table::loginTable(User *_p){
	assert( _p != NULL );
	addLookUser(_p);
	return 0;
}

int Table::logoutTable(User *_p){
	assert( _p != NULL );
	delLookUser(_p);
	return 0;
}

int Table::sitTable(User *_p, int seatid){
	assert( _p != NULL );
	assert( seatid >=0 && seatid < 3 );
	if( tableSeat[seatid].isHaveUser() ){
		//座位已经有人了。
		return 0;
	}
	delLookUser(_p);
	tableSeat[seatid].sit(_p);
	return 0;
}

int Table::standTable(User *_p){
	assert( _p != NULL );
	int seatid = _p->seatid;
	if( !isValidSeatid(seatid) ){
		//用户没有坐下，
		return 0;
	}
	Seat &s = tableSeat[seatid];
	s.stand();
	addLookUser(_p);
	return 0;
}
//游戏开始事件
int Table::readyPlay(User *_p){
	int seatid = _p->seatid;
	if( !isValidSeatid(seatid) ){
		return 0;
	}
	Seat &s = tableSeat[seatid];
	s.ready = true;
	printf("user is ready, seatid:%d\n", seatid);
	//检测游戏是否开始
	for(unsigned int i=0; i<3; i++){
		Seat &s2 = tableSeat[i];
		if( !s2.isReady() ){
			printf("not all user ready seatid:%d\n", i);
			return 0;
		}
	}
	//开始游戏
	startTableGame();
	return 0;
}

void Table::initStartGame(){
	for(unsigned int i=0; i<3; i++){
		Seat &s = tableSeat[i];
		s.initStartGame();
	}
}

void Table::startTableGame(){
	//判断游戏是否能开始
	printf("Game Start\n");
	for(unsigned int i=0; i<3; i++){
		Seat &s = tableSeat[i];
		if( !s.isHaveUser() ){
			return ;
		}
	}
	//游戏可以正常开始 发牌逻辑啦

	initStartGame();
	bid++;
	shufflePoker(tableSeat[(dealerSeat)%3].pokers, tableSeat[(dealerSeat+1)%3].pokers, tableSeat[(dealerSeat+2)%3].pokers, bottomPoker);
	for(unsigned int i = 0; i < 3; i++){
		Seat &s = tableSeat[i];
		User *u = s.pUser;//assert is not NULL
		ESOutputPacket output;
		output.Begin(SRV_GAME_USER_POKER);
		for(unsigned int j=0; j<17; j++){
			output.WriteShort(s.pokers[j].getPoker());
		}
		output.End();
		u->sendPacketData(output);
	}
}

void Table::overTableGame(){
	//1、游戏正常结束；
	//2、还有可能是用户站起结束；
	bool isNormalOver = true;
	int winRole = -1;
	calcSeatWin tmpSeat[3];
	for(unsigned int i=0; i<3; i++){//同步临时数据
		tmpSeat[i].seatid = i;
		Seat &s = getSeatFromUser(i);
		if( s.isHaveUser() ){
			tmpSeat[i].HaveUser = true;
		}else{
			tmpSeat[i].HaveUser = false;
			isNormalOver = false;
			winRole = 1 - s.role;//因为role只有0和1，所以1减去role的话，一定是相反的值
		}
		tmpSeat[i].role = s.role;
		tmpSeat[i].pokerSize = s.getPokerNum();
		if( tmpSeat[i].pokerSize == 0 ){
			winRole = tmpSeat[i].role;
		}
	}
	//数据已经同步完成了。下面是评判得分系统
}


//打牌过程中的函数
int Table::chipinCard(User *_p, pokerVec &pokers){
	assert( _p != NULL );
	int seatid = _p->seatid;
	if( !isValidSeatid(seatid) ){
		//用户没有坐下
		return 0;
	}
	Seat &s = getSeatFromUser(seatid);
	pokerVec& userPoker = s.pokers;
	
	if( !pokers.isInPokers( userPoker ) ){
		//不是你的牌
		return 0;
	}
	
	if( userPoker.isBiggerThan( lastHandPoker ) ){//最重要的函数
		//出的牌有误
		return 0;
	}

	//牌已经校验完毕，现在需要同步数据
	//userPoker -= pokers;
	if( userPoker.size() == 0 ){//判断用户是否牌结束了，是否游戏结束
		overTableGame();
	}
	return 0;
}

Seat& Table::getSeatFromUser(int seatid){//必须是有效seat
	assert( isValidSeatid(seatid) );
	return tableSeat[seatid];
}

bool Table::isValidSeatid(int seatid){
	return ((seatid>=0) && (seatid<3));
}

void Table::delLookUser(User *_p){
	for(unsigned int i=0; i<lookUsers.size(); i++){
		if( _p == lookUsers[i] ){
			//lookUsers.erase(i);
		}
	}
}

void Table::addLookUser(User *_p){
	lookUsers.push_back(_p);
}

void Table::__TableInfo__(){
	int a[3] = {0};
	for (int i=0; i<3 ;i++ )
	{
		User *_p = tableSeat[i].pUser;
		if(_p == NULL){
			a[i] = 0;
		}else{
			a[i] = _p->uid;
		}
	}
	printf("%d:users:%d, %d, %d", tid, a[0], a[1], a[2]);
	unsigned int max = lookUsers.size();
	printf("\t\t lookUser tid :%d | ", tid);
	for (unsigned int i=0; i< max; i++)
	{
		printf("%d",lookUsers[i]->uid);
	}
	printf("\n");
}

